package com.controller;

import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.entity.XiaoshuoxinxiEntity;
import com.entity.view.XiaoshuoxinxiView;
import com.entity.vo.GoumaixiaoshuoVO;
import com.entity.vo.XiaoshuoxinxiVO;
import com.service.GoumaixiaoshuoService;
import com.service.XiaoshuoxinxiService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 小说信息
 * 后端接口
 *
 * @author
 * @email
 * @date 2021-03-07 10:13:33
 */
@RestController
@RequestMapping("/xiaoshuoxinxi")
public class XiaoshuoxinxiController {
    @Autowired
    private XiaoshuoxinxiService xiaoshuoxinxiService;
    @Autowired
    private GoumaixiaoshuoService goumaixiaoshuoService;


    /**
     * 推荐
     */
    @IgnoreAuth
    @RequestMapping("/tuijian")
    public R tuijian(@RequestParam Map<String, Object> params, XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request, String pre) {

        String myDuZhe = (String) request.getSession().getAttribute("myDuZhe");
        List<GoumaixiaoshuoVO> goumaixiaoshuoVOS = new ArrayList<>();
        List<GoumaixiaoshuoVO> aLLBooks = new ArrayList<>();
        HashMap<String,String> aLLBooksMap = new HashMap<String,String>();
        if (myDuZhe != null && myDuZhe.length() != 0) {
            goumaixiaoshuoVOS = goumaixiaoshuoService.beforeThreeBook(myDuZhe);
            aLLBooks = goumaixiaoshuoService.beforeBooks(myDuZhe);
            //将List存储为map
            for (GoumaixiaoshuoVO aLLBook : aLLBooks) {
                aLLBooksMap.put(aLLBook.getXiaoshuomingcheng(),aLLBook.getXiaoshuomingcheng());
            }
        }


        //根据类型推荐
        if (goumaixiaoshuoVOS != null && goumaixiaoshuoVOS.size() != 0) {
            List<XiaoshuoxinxiVO> data = new ArrayList<>();
            HashMap<String,String> bookNameMap = new HashMap<>();
            //获取同类型的书
            List<Map<String, String>> bookByType = xiaoshuoxinxiService.getBookByType(goumaixiaoshuoVOS.get(0).getXiaoshuofenlei());

            for (Map<String, String> stringMap : bookByType) {
                //书小于三本
                if (data.size()<=3){
                    //没有买过这本书
                    if (!aLLBooksMap.containsKey(stringMap.get("xiaoshuomingcheng"))){
                        String bookName = stringMap.get("name");
                        bookNameMap.put(bookName,bookName);
                        Map<String, Object> myMap = xiaoshuoxinxiService.getXiaoShuoByName(bookName);
                        XiaoshuoxinxiVO xiaoShuoByName = new XiaoshuoxinxiVO(
                                Long.valueOf((String) myMap.get("id")),
                                (String)( myMap.get("xiaoshuomingcheng")),
                                (String) myMap.get("xiaoshuofenlei"),
                                (String) myMap.get("jianjie"),
                                Integer.valueOf((String) myMap.get("zishu")) ,
                                Integer.valueOf((String) myMap.get("jiage")) ,
                                (String) myMap.get("xiaoshuofenmian"),
                                (String) myMap.get("zuozhezhanghao"),
                                (String) myMap.get("zuozhexingming"),
                                (Integer) myMap.get("thumbsupnum"),
                                (Integer) myMap.get("crazilynum"),
                                (Date) null,
                                (Integer) myMap.get("clicknum")
                        );
                        data.add(xiaoShuoByName);

                        aLLBooksMap.put(stringMap.get("xiaoshuomingcheng"),stringMap.get("xiaoshuomingcheng"));
                    }
                }

            }

            //根据大数据算法推荐
            List<Map<String, String>> moreForName = goumaixiaoshuoService.getMoreForName(myDuZhe);
            Integer num = 0;
            for (Map<String, String> stringMap : moreForName) {
                num++;
                if (num <= 6 - bookByType.size()) {
                    String bookName = stringMap.get("name");
                    if (aLLBooksMap.containsKey(bookName)){
                        num--;
                        continue;
                    }
                    Map<String, Object> myMap =xiaoshuoxinxiService.getXiaoShuoByName(bookName);
                    XiaoshuoxinxiVO xiaoShuoByName = new XiaoshuoxinxiVO(
                            Long.valueOf((String) myMap.get("id")),
                            (String)( myMap.get("xiaoshuomingcheng")),
                            (String) myMap.get("xiaoshuofenlei"),
                            (String) myMap.get("jianjie"),
                            Integer.valueOf((String) myMap.get("zishu")) ,
                            Integer.valueOf((String) myMap.get("jiage")) ,
                            (String) myMap.get("xiaoshuofenmian"),
                            (String) myMap.get("zuozhezhanghao"),
                            (String) myMap.get("zuozhexingming"),
                            (Integer) myMap.get("thumbsupnum"),
                            (Integer) myMap.get("crazilynum"),
                            (Date) null,
                            (Integer) myMap.get("clicknum")
                    );
                    data.add(xiaoShuoByName);
                }
            }


            PageUtils pageUtils = new PageUtils(data, 6, 6, 1);
            return R.ok().put("data", pageUtils);
        } else {
            EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
            Map<String, Object> newMap = new HashMap<String, Object>();
            Map<String, Object> param = new HashMap<String, Object>();
            Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                String key = entry.getKey();
                String newKey = entry.getKey();
                if (pre.endsWith(".")) {
                    newMap.put(pre + newKey, entry.getValue());
                } else if (StringUtils.isEmpty(pre)) {
                    newMap.put(newKey, entry.getValue());
                } else {
                    newMap.put(pre + "." + newKey, entry.getValue());
                }
            }
            params.put("sort", "clicknum");
            params.put("order", "desc");
            PageUtils page = xiaoshuoxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xiaoshuoxinxi), params), params));
            return R.ok().put("data", page);
        }
    }


    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request) {

        EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
        PageUtils page = xiaoshuoxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xiaoshuoxinxi), params), params));
        request.setAttribute("data", page);
        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request) {
        EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
//    	PageUtils page = xiaoshuoxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xiaoshuoxinxi), params), params));


//        String myDuZhe = (String) request.getSession().getAttribute("myDuZhe");
//        if (myDuZhe!=null&&myDuZhe.length()!=0){
//            List<XiaoshuoxinxiVO> data = new ArrayList<>();
//            List<Map<String, String>> moreForName = goumaixiaoshuoService.getMoreForName(myDuZhe);
//            Integer num = 0 ;
//            for (Map<String, String> stringMap : moreForName) {
//                num++;
//                if (num<=6){
//                    String bookName = stringMap.get("name");
//                    XiaoshuoxinxiVO xiaoShuoByName = xiaoshuoxinxiService.getXiaoShuoByName(bookName);
//                    data.add(xiaoShuoByName);
//                }
//            }
//            PageUtils pageUtils =new PageUtils(data,6,6,1);
//            return R.ok().put("data", pageUtils);
//        }else {
        PageUtils page = xiaoshuoxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xiaoshuoxinxi), params), params));
        return R.ok().put("data", page);
//        }
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(XiaoshuoxinxiEntity xiaoshuoxinxi) {
        EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
        ew.allEq(MPUtil.allEQMapPre(xiaoshuoxinxi, "xiaoshuoxinxi"));
        return R.ok().put("data", xiaoshuoxinxiService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(XiaoshuoxinxiEntity xiaoshuoxinxi) {
        EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
        ew.allEq(MPUtil.allEQMapPre(xiaoshuoxinxi, "xiaoshuoxinxi"));
        XiaoshuoxinxiView xiaoshuoxinxiView = xiaoshuoxinxiService.selectView(ew);
        return R.ok("查询小说信息成功").put("data", xiaoshuoxinxiView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        XiaoshuoxinxiEntity xiaoshuoxinxi = xiaoshuoxinxiService.selectById(id);
        xiaoshuoxinxi.setClicknum(xiaoshuoxinxi.getClicknum() + 1);
        xiaoshuoxinxi.setClicktime(new Date());
        xiaoshuoxinxiService.updateById(xiaoshuoxinxi);
        return R.ok().put("data", xiaoshuoxinxi);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        XiaoshuoxinxiEntity xiaoshuoxinxi = xiaoshuoxinxiService.selectById(id);
        xiaoshuoxinxi.setClicknum(xiaoshuoxinxi.getClicknum() + 1);
        xiaoshuoxinxi.setClicktime(new Date());
        xiaoshuoxinxiService.updateById(xiaoshuoxinxi);
        return R.ok().put("data", xiaoshuoxinxi);
    }


    /**
     * 赞或踩
     */
    @RequestMapping("/thumbsup/{id}")
    public R vote(@PathVariable("id") String id, String type) {
        XiaoshuoxinxiEntity xiaoshuoxinxi = xiaoshuoxinxiService.selectById(id);
        if (type.equals("1")) {
            xiaoshuoxinxi.setThumbsupnum(xiaoshuoxinxi.getThumbsupnum() + 1);
        } else {
            xiaoshuoxinxi.setCrazilynum(xiaoshuoxinxi.getCrazilynum() + 1);
        }
        xiaoshuoxinxiService.updateById(xiaoshuoxinxi);
        return R.ok("投票成功");
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request) {
        xiaoshuoxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        //ValidatorUtils.validateEntity(xiaoshuoxinxi);

        xiaoshuoxinxiService.insert(xiaoshuoxinxi);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request) {
        xiaoshuoxinxi.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        //ValidatorUtils.validateEntity(xiaoshuoxinxi);

        xiaoshuoxinxiService.insert(xiaoshuoxinxi);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(xiaoshuoxinxi);
        xiaoshuoxinxiService.updateById(xiaoshuoxinxi);//全部更新
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        xiaoshuoxinxiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 提醒接口
     */
    @RequestMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
                         @PathVariable("type") String type, @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        if (type.equals("2")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if (map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if (map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        Wrapper<XiaoshuoxinxiEntity> wrapper = new EntityWrapper<XiaoshuoxinxiEntity>();
        if (map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if (map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }


        int count = xiaoshuoxinxiService.selectCount(wrapper);
        return R.ok().put("count", count);
    }


    /**
     * 前端智能排序
     */
    @IgnoreAuth
    @RequestMapping("/autoSort")
    public R autoSort(@RequestParam Map<String, Object> params, XiaoshuoxinxiEntity xiaoshuoxinxi, HttpServletRequest request, String pre) {
        EntityWrapper<XiaoshuoxinxiEntity> ew = new EntityWrapper<XiaoshuoxinxiEntity>();
        Map<String, Object> newMap = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            String newKey = entry.getKey();
            if (pre.endsWith(".")) {
                newMap.put(pre + newKey, entry.getValue());
            } else if (StringUtils.isEmpty(pre)) {
                newMap.put(newKey, entry.getValue());
            } else {
                newMap.put(pre + "." + newKey, entry.getValue());
            }
        }
        params.put("sort", "clicknum");
        params.put("order", "desc");
        PageUtils page = xiaoshuoxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, xiaoshuoxinxi), params), params));
        return R.ok().put("data", page);
    }


}
